package com.jiangyao.controller;

import com.jiangyao.common.ApiResult;
import com.jiangyao.common.constants.FileConstants;
import com.jiangyao.common.utils.Base64Util;
import com.jiangyao.common.utils.FtpClient;
import com.jiangyao.common.utils.StringUtils;
import com.jiangyao.model.vo.FileVO;
import com.jiangyao.model.vo.ProvinceVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 通用 前端控制器
 * </p>
 *
 * @author yych
 * @since 2020-06-05
 */
@Api(tags = "通用接口")
@RestController
@RequestMapping("/common")
public class CommonController extends BaseController {

	@RequestMapping("/start")
	public ApiResult<String> start() {
		return new ApiResult<>("匠要-前台api-运行中");
	}

	@Autowired
	private FtpClient ftpClient;


	/**
	 * base64 图片上传(传的是一个字符串)
	 *
	 * @param upfile 图片base64
	 * @return 结果
	 * @throws Exception
	 */
	@ApiOperation("base64 图片上传")
	@RequestMapping(value = "/upload_base64", method = RequestMethod.POST)
	@ResponseBody
	public ApiResult uploadImgBase64(@RequestParam(defaultValue = "1") String upfile) throws Exception {
		if (StringUtils.isBlank(upfile)) {
			return ApiResult.error(100, "请选择图片上传");
		}
		String format = null;
		if (upfile.contains("data:image/jpeg;base64,")) {
			format = "jpg";
			upfile = upfile.replaceAll("data:image/jpeg;base64,", "");
		} else if (upfile.contains("data:image/png;base64,")) {
			format = "png";
			upfile = upfile.replaceAll("data:image/png;base64,", "");
		} else if (upfile.contains("data:image/gif;base64,")) {
			format = "gif";
			upfile = upfile.replaceAll("data:image/tiff;base64,", "");
		} else if (upfile.contains("data:image/bmp;base64,")) {
			format = "bmp";
			upfile = upfile.replaceAll("data:image/bmp;base64,", "");
		} else {
			return ApiResult.error(100, "只支持上传png、jpg、gif、bmp文件");
		}
		byte[] bytes = Base64Util.decode(upfile);
		for (int i = 0; i < bytes.length; ++i) {
			if (bytes[i] < 0) {// 调整异常数据
				bytes[i] += 256;
			}
		}
		//上传文件
		String path = ftpClient.uploadImg(bytes, format);
		return ApiResult.success(FileVO.builder().path(path).url(FtpClient.fullFtpUrl(path)).suffix(format).build());
	}


	/**
	 * 文件上传:图片、视频等文件
	 *
	 * @param file 上传的文件
	 */
	@ApiOperation("文件上传:图片、视频等文件")
	@ApiImplicitParam(name = "file", value = "文件", dataTypeClass = MultipartFile.class)
	@PostMapping(value = "/upload")
	@ResponseBody
	public ApiResult<FileVO> uploadFile(MultipartFile file) throws Exception {
		if (file == null) {
			return ApiResult.error("文件不能为空");
		}
		String path = null;
		String name = file.getOriginalFilename();
		String suffix = FilenameUtils.getExtension(name);
		if (StringUtils.isNotEmpty(suffix) && FileConstants.IMAGE_SUFFIX_LIST.contains(suffix.toLowerCase())) {
			path = ftpClient.uploadImg(file.getBytes(), suffix);
		} else if (StringUtils.isNotEmpty(suffix) && FileConstants.VIDEO_SUFFIX_LIST.contains(suffix.toLowerCase())) {
			path = ftpClient.uploadVideo(file.getBytes(), suffix);
		} else {
			path = ftpClient.uploadFile(file.getInputStream(), suffix);
		}
		return ApiResult.success(FileVO.convertFileVO(file, path));
	}


	/**
	 * 文件下载:图片、视频等文件
	 *
	 * @param response 响应参数
	 * @param url      源文件访问地址，绝对地址
	 * @param fileName 下载文件名称
	 * @return 返回的是全路径
	 * @throws Exception 异常
	 */
	@ApiOperation("文件下载:图片、视频等文件")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "url", value = "源文件访问地址，绝对地址"),
			@ApiImplicitParam(name = "fileName", value = "下载文件名称")
	})
	@GetMapping(value = "/download")
	public ApiResult<Object> downloadByFileUrl(HttpServletResponse response, @RequestParam String url
			, @RequestParam String fileName) throws Exception {
		ftpClient.downloadByUrl(response, url, fileName);
		return ApiResult.success("success");
	}

}

